package com.example.room;

import android.app.Application;
import android.os.AsyncTask;

import androidx.lifecycle.LiveData;

import java.util.List;

public class WordRepository {
    private WordDao wordDao;
    private LiveData<List<Word>> allWords;

    WordRepository(Application application) {
        WordRoomDatabase database = WordRoomDatabase.getDatabase(application);
        wordDao = database.wordDao();
        allWords = wordDao.getAllWords();
    }
//添加获取全部单词方法
    public LiveData<List<Word>> getAllWords()
    {
        return allWords;
    }
    public void insert(Word word) {


//        wordDao.insert(word);
 new InsertAsyncTask(wordDao).execute(word);
    }
    private static class InsertAsyncTask extends AsyncTask<Word,Void,Void>{
        private WordDao nAsyncTaskDao;

        public InsertAsyncTask(WordDao nAsyncTaskDao) {
            this.nAsyncTaskDao=nAsyncTaskDao;

        }

        @Override
        protected Void doInBackground(Word... words) {

            nAsyncTaskDao.insert(words[0]);

            return null;
        }
    }
}
